﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Delonix_Regia_Hotel_Management_System.Class;
using System.Data.SqlClient;
using System.Configuration;

namespace Delonix_Regia_Hotel_Management_System
{
    public partial class Housekeeping_and_Staff_Information_A_ : Form
    {
        SqlConnection conn = new SqlConnection();

        SqlCommand comm = new SqlCommand();
        public Housekeeping_and_Staff_Information_A_()
        {
            InitializeComponent();
        }

        private void btnRetrieveAll_Click(object sender, EventArgs e)
        {
            List<HouseKeeping> houseKeeping = DBManager.getAllHouseKeeping();
            dataGridView1.DataSource = houseKeeping;
            if (houseKeeping.Count > 0)
            {
                //DataGridViewCheckBoxColumn reorderCol = new DataGridViewCheckBoxColumn();
                //dataGridView1.Columns.Add(reorderCol);
            }

            //DataGridViewCheckBoxColumn reorderCol1 = new DataGridViewCheckBoxColumn();
            //dataGridView1.Columns.Add(reorderCol1);
            
        }

        private void Bind()
        {
            conn.ConnectionString = "data source=localhost; initial catalog=DelonixRegia; user id=user; password=Password_2013";
            conn.Open();
            SqlDataAdapter da = new SqlDataAdapter("select * Housekeeping", conn);
            //DataTable dt = new DataTable();
            //da.Fill(dt);
            dataGridView1.DataSource = da;
            conn.Close();
        }

        private void BtnUpdate_Click(object sender, EventArgs e)
        {
            conn.ConnectionString = "data source=localhost; initial catalog=DelonixRegia; user id=user; password=Password_2013";
            conn.Open();
            for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
            {
                SqlCommand cmd = new SqlCommand("Update Housekeeping set dutyType=@dutyType,scheduleDate=@scheduleDate,staffId=@staffId Where(staffId=@staffId)", conn);
                cmd.Parameters.AddWithValue("@dutyType", dataGridView1.Rows[i].Cells[0].Value.ToString());
                cmd.Parameters.AddWithValue("@scheduleDate", Convert.ToDateTime(dataGridView1.Rows[i].Cells[1].Value.ToString()));
                cmd.Parameters.AddWithValue("@staffId", dataGridView1.Rows[i].Cells[2].Value.ToString());
                cmd.ExecuteNonQuery();
            }
            conn.Close();
            MessageBox.Show("updated");
            Bind();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Do you really want to delete selected record(s)?",
               "Delete warning", MessageBoxButtons.YesNo,
               MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2, 0, false)
               == DialogResult.Yes)
            {
                conn.ConnectionString = "data source=localhost; initial catalog=DelonixRegia; user id=user; password=Password_2013";
                SqlCommand delcmd = new SqlCommand();
                String staffID = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
                if (dataGridView1.Rows.Count > 1)
                {
                    delcmd.CommandText = "DELETE FROM Housekeeping WHERE staffId=@staffID";
                    conn.Open();
                    delcmd.Connection = conn;
                    delcmd.Parameters.AddWithValue("@staffId", staffID);
                    delcmd.ExecuteNonQuery();
                    conn.Close();
                    MessageBox.Show("Row Deleted");
                }
                Bind();
            }
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form1 F = new Form1();
            this.Hide();
            F.Show(); 
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            Main__Management_and_Administrator_ A = new Main__Management_and_Administrator_();
            this.Hide();
            A.Show(); 
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form1 F = new Form1();
            this.Hide();
            F.Show(); 
            
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            string dutyType = Convert.ToString(comboBoxDutyType.SelectedItem);
            DateTime sheduleDate = Convert.ToDateTime(dateTimePicker1.Value.Date.Year + "/" + dateTimePicker1.Value.Date.Month + "/" + dateTimePicker1.Value.Date.Day);
            string staffId = tbxStaffId.Text;

            HouseKeeping HK = new HouseKeeping(dutyType,sheduleDate,staffId);

            if (DBManager.CreateHousekeeping(HK) == 1)
            {
                MessageBox.Show("Staff successfully created!");
                Housekeeping_and_Staff_Information_A_ A = new Housekeeping_and_Staff_Information_A_();
                this.Hide();
                A.Show();
            }
        }
    }
}
